package com.demo.easyExcel.fillColor.version3_3_2;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 *
 *   3.3.2的版本实现  写入excel。  并且设置excel表格 某一个单元格的颜色。  注意不是根据模板，就是单纯的写数据
 */
public class Demo4ForCSDN {

    @Test
    public void test() throws FileNotFoundException {
        // 文件输出位置
        OutputStream out = new FileOutputStream("list2"  + ".xlsx");

        // 指定要高亮的单元格
        //用来记录需要为第`key`行中的第`value.get(i)`列设置样式   设置需要处理的单元格
        HashMap<Integer, List<Integer>> map = new HashMap<>();
        // 第1行，第2，5列的格子高亮
        map.put(1, Arrays.asList(2, 5));
        map.put(2, Arrays.asList(1, 2, 4));
        map.put(4, Arrays.asList(1, 2, 4));
        //指定单元格样式,通过有残构造，指定填充的颜色
        CellColorCellWriteHandler writeHandler = new CellColorCellWriteHandler(map, IndexedColors.YELLOW1.getIndex());
        ExcelWriter excelWriter = null;
        try {
            excelWriter = EasyExcel
                    .write(out, Person.class)  // Person指定了表头
//                    .registerWriteHandler(defaultStyles())
                    .registerWriteHandler(writeHandler) // 注意优先级，后面的writeHandler会覆盖前面的样式
                    .build();
            WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
            // 第一个参数传List，泛型可以是person实例或者一个List
//            excelWriter.write(personData(), writeSheet);
            // 如果泛型是List，就失去隐藏表头的功能，所有字段都写到excel
            excelWriter.write(listData(), writeSheet);
        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    private List<List<Object>> listData() {
        List<List<Object>> lists = new ArrayList<>();
        lists.add(Lists.newArrayList("张三","男",20,"游泳","北京","单身"));
        lists.add(Lists.newArrayList("李四","男",20,"游泳","北京","单身"));
        lists.add(Lists.newArrayList("王五","男",20,"游泳","北京","单身"));
        lists.add(Lists.newArrayList("赵薇","女",25,"游泳","北京","单身","明星"));
        return lists;
    }


    /**
     * 默认样式
     */
    public static HorizontalCellStyleStrategy defaultStyles() {
        //默认样式
        //表头样式策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置表头居中对齐
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //表头前景设置淡蓝色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setBold(true);
        headWriteFont.setFontName("宋体");
        headWriteFont.setFontHeightInPoints((short) 12);
        headWriteCellStyle.setWriteFont(headWriteFont);

        //内容样式策略策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 设置背景颜色白色
        contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        // 设置垂直居中为居中对齐
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置左右对齐为靠左对齐
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        // 设置单元格上下左右边框为细边框
        contentWriteCellStyle.setBorderBottom(BorderStyle.MEDIUM);
        contentWriteCellStyle.setBorderLeft(BorderStyle.MEDIUM);
        contentWriteCellStyle.setBorderRight(BorderStyle.MEDIUM);
        contentWriteCellStyle.setBorderTop(BorderStyle.MEDIUM);
        //创建字体对象
        WriteFont contentWriteFont = new WriteFont();
        //内容字体大小
        contentWriteFont.setFontName("宋体");
        contentWriteFont.setFontHeightInPoints((short) 14);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        
        // 初始化表格样式
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }
}
